package com.gs.controller;

import com.gs.pojo.Attribute;
import com.gs.pojo.Commodity;
import com.gs.service.attribute.AttributeService;
import com.gs.service.commodity.CommodityService;
import com.gs.utils.Page;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/Commodity")
public class CommodityController {

  @Autowired
  private CommodityService commodityService;

  @Autowired
  private AttributeService attributeService;
//	@RequestMapping("/tolist")
//	public ModelAndView tolist(){
//		ModelAndView mv=null;
//		mv=new ModelAndView("/commodity/list");
//		return mv;
//	}

  //分页和模糊查询
  @RequestMapping("/tolist")
  public ModelAndView list(HttpServletRequest request, Integer currentPage, String txtname, Integer commodityTypeID) {
    ModelAndView mv = null;
			List<Attribute> listOne=attributeService.selectCommodityType();
    mv = new ModelAndView("/commodity/list");
    Page<Commodity> vo = new Page<Commodity>();
    if (commodityTypeID == null) {
      commodityTypeID = 16;
    }
    if (currentPage == null) {
      currentPage = 1;
    } else if (currentPage == 0) {
      currentPage = 1;
    }
    if (txtname == null) {
      txtname = "";
    }
    vo.setCurrentPage(currentPage);
    vo = this.commodityService.pageFuzzyselect(txtname, commodityTypeID, vo);
    mv.addObject("list", vo);
    mv.addObject("txtname", txtname);
//			mv.addObject("listOne",listOne);
    mv.addObject("commodityType", commodityTypeID);
    return mv;
  }


  @RequestMapping("/toadd")
  public ModelAndView toadd() {
    ModelAndView mv = null;
    List<Attribute> listOne = attributeService.selectUOM();
    List<Attribute> listTwo = attributeService.selectCommodityType();
    mv = new ModelAndView("/commodity/add");
    mv.addObject("listOne", listOne);
    mv.addObject("listTwo", listTwo);
    return mv;
  }

  @RequestMapping("/add")
  public ModelAndView add(Commodity commodityPo) {
    ModelAndView mv = null;
    commodityService.insertAll(commodityPo);
    mv = new ModelAndView("redirect:/Commodity/tolist.do");
    return mv;
  }

  @RequestMapping("/toupdate")
  public ModelAndView toupdate(int id) {
    ModelAndView mv = null;
    List<Attribute> listOne = attributeService.selectUOM();
    List<Attribute> listTwo = attributeService.selectCommodityType();
    Commodity commodityPo = commodityService.selectById(id);
    mv = new ModelAndView("/commodity/update");
    mv.addObject("listOne", listOne);
    mv.addObject("listTwo", listTwo);
    mv.addObject("listPo", commodityPo);
    return mv;
  }

  @RequestMapping("/update")
  public ModelAndView update(Commodity commodityPo) {
    ModelAndView mv = null;
    commodityService.updateById(commodityPo);
    mv = new ModelAndView("redirect:/Commodity/tolist.do");
    return mv;
  }

  @RequestMapping("/delete")
  public ModelAndView delete(String id) {
    ModelAndView mv = null;
    String[] FenGe = id.split(",");
    for (int i = 0; i < FenGe.length; i++) {
      commodityService.deleteById(Integer.parseInt(FenGe[i]));
    }
    mv = new ModelAndView("redirect:/Commodity/tolist.do");
    return mv;
  }


  @RequestMapping("/openwindow")
  public ModelAndView openwindow() {
    ModelAndView mv = null;
    mv = new ModelAndView("/commodity/commoditytype");
    return mv;
  }

  @RequestMapping("/newadd")
  public ModelAndView newadd(String txtname) {
    ModelAndView mv = null;
    int newid = 3;
    attributeService.insertAll(newid, txtname);
    mv = new ModelAndView("redirect:/Commodity/tolist.do");
    return mv;
  }

  @RequestMapping("/newdelete")
  public ModelAndView newdelete(String id) {
    ModelAndView mv = null;
    String[] FenGe = id.split(",");
    for (int i = 0; i < FenGe.length; i++) {
      attributeService.deleteById(Integer.parseInt(FenGe[i]));
    }
    mv = new ModelAndView("redirect:/Commodity/tolist.do");
    return mv;
  }



}

